package job.tracker.service

import job.tracker.domain.JobSite;
import org.jsoup.*;
import org.jsoup.nodes.*;
import org.jsoup.safety.*;
import org.jsoup.select.*;

class UtilService {

	def grailsApplication
	
	List<String> scrapeJobs(JobSite jobSite) {
		def jSoupConfig = grailsApplication.config.jobTracker.jSoup

		println "making a hit to " + jobSite.pageUrl + " at " + new Date();
		Connection conn = Jsoup.connect(jobSite.pageUrl)
				.userAgent(jSoupConfig.userAgent)
				.timeout(jSoupConfig.timeout)
				
		Document doc = (jobSite.postDataParams) ? conn.data(jobSite.postDataParams).post() : conn.get();

		Element content = doc.body()
		
		List<String> contentSelectors = jobSite.dataElement.tokenize('|')

		contentSelectors.each { selector ->
			selector = selector.trim()
			
			content = Selector.select(selector, content).first()
		}
				
		if (!content) {
			//TODO: log and email
			println "${jobSite.pageUrl} - dataElement: ${jobSite.dataElement} - jobSelector: ${jobSite.jobSelector} no content found. id: ${jobSite.id} "
			return [];
		}
		
		Elements jobs = []
		
		if (jobSite.jobSelector) {
			jobs = content.select(jobSite.jobSelector)
			
			if (!jobs.size()) {
				//TODO: log and email
				println "${jobSite.pageUrl} - dataElement: ${jobSite.dataElement} - jobSelector: ${jobSite.jobSelector} individual selecter didnt return anything id: ${jobSite.id} "
				return [];
			}
		}
		
		List<String> cleanedJobsHtml = jobs.collect {
			Jsoup.clean(it.html(), Whitelist.basic());
		}
		
		return cleanedJobsHtml;
	}
}
